<?php

!defined('DEBUG') AND exit('Access Denied.');

$action = param(1);

if(!admin_authority_check()) header("Location:".url('index-login'));

if($action == 'cash'){

	if($method=='GET'){
		//参数
		$status = param('status', '-1');
		$number = param('number', '');
		$order_username = param('order_username', '');
		$fdate = param('fdate', date('m/d/Y', strtotime('today')));	// 开始时间
		$tdate = param('tdate', date('m/d/Y', strtotime('today')));	// 结束时间
		$export = param('export', 0);	// 导出excel
		//条件
		$cond = "where uid in(select uid from ".$db->tablepre."finance_power where fid=".$user['uid'].") and time>=".strtotime($fdate)." and time<".(strtotime($tdate)+86400);
		if($status!=-1) $cond.= " and status='$status'";
		if($number!='') $cond.= " and number='$number'";
		if($order_username!='') $cond.= " and username='$order_username'";
		//导出excel
		if($export){
			$log = db_sql_find("SELECT * FROM ".$db->tablepre."order_cash $cond order by time desc limit 10000");
			if(!empty($log)){
				//colname
				$colname = array('订单号','商户账户','金额','手续费','银行','银行账号','收款人','提现时间','订单状态','提现类型','处理人','处理时间');
				//data
				$data = array();
				foreach($log as $k=>$v){
					$data[$k]['number'] = ' '.$v['number'];//订单号
					$data[$k]['username'] = ' '.$v['username'];//商户账户
					$data[$k]['coin'] = $v['coin'];//金额
					$data[$k]['fee'] = $v['fee'];//手续费
					$data[$k]['bank'] = $v['bank'].($v['is_dai']==1?'(代付)':'');//银行
					$data[$k]['code'] = ' '.$v['code'];//银行账号
					$data[$k]['name'] = ' '.$v['name'];//收款人
					$data[$k]['time'] = date('Y-m-d H:i:s', $v['time']);//提现时间
					$data[$k]['status'] = $v['status'];//订单状态
					//提现类型
					$data[$k]['type'] = '';
					if($v['type']==1) $data[$k]['type'] = '通道:'.$v['type_name'];
					elseif($v['type']==2) $data[$k]['type'] = '银行:'.$v['type_name'];
					//
					$data[$k]['do_username'] = ' '.$v['do_username'];//处理人
					$data[$k]['do_time'] = ($v['do_time']?date('Y-m-d H:i:s', $v['do_time']):'');//处理时间
				}
				//
				$title = "提现记录 ($fdate-$tdate)";
				$filename = '提现记录';
				export_excel($title,$colname,$data,$filename);//下载excel
			}else{
				exit('没有数据');
			}
		}
		
		file_put_contents('../tmp/remind-cash-'.$user['uid'].'.txt', '0');
		$channels = db_find('channel', array(), array(), 1, 300);
		//统计
		$total = db_sql_find_one("SELECT sum(coin) as cash_total,sum(fee) as cash_fee FROM ".$db->tablepre."order_cash $cond");
		//
		$page = param('page', 1);
		$page = max(1, $page);
		$pagesize = 100;
		$offset = ($page - 1) * $pagesize;
		$log = db_sql_find("SELECT * FROM ".$db->tablepre."order_cash $cond order by time desc limit $offset,$pagesize");
		$param = "?status=".$status.'&number='.$number.'&order_username='.$order_username.'&fdate='.$fdate.'&tdate='.$tdate;
		$count = db_sql_find_one("SELECT count(id) as total FROM ".$db->tablepre."order_cash $cond");
		$count = !empty($count)?$count['total']:0;
		$pagination = pagination(url("order-cash").$param."&page={page}", $count, $page, $pagesize);
		$header['title'] = '提现订单 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/order_cash.htm");
	
	}else{
		
		$status = urldecode(param(2));
		$id = param(3);
		$order = order_cash_read($id);
		if(!$order) message(0, '订单无效');
		//权限检查
		$power = db_find_one('finance_power',array('fid'=>$user['uid'], 'uid'=>$order['uid']));
		if(empty($power)) message(0, '无权限');
		//
		if($order['status']!='待审核') message(0, '订单状态错误');
		$order_cash=array();
		$order_cash['status'] = $status;
		$order_cash['do_username'] = $user['username'];
		$order_cash['do_time'] = $time;
		if($status=='已拒绝'){
        	$coin = $order['is_dai']==1 ? $order['coin'] + $order['fee'] : $order['coin'];
        	user__update($order['uid'], array('coin+'=>$coin));
        }elseif($status=='已审核'){
			$type_channel = param('type_channel');
			$type_bank = param('type_bank');
			if($type_channel=='' && $type_bank=='') message(0, '请选择通道或银行');
			elseif($type_channel!='' && $type_bank!='') message(0, '只能选择通道或银行其中一个');
			elseif($type_channel!=''){
				$order_cash['type'] = 1;
				$order_cash['type_name'] = $type_channel;
			}elseif($type_bank!=''){
				$order_cash['type'] = 2;
				$order_cash['type_name'] = $type_bank;
			}
			user__update($order['uid'], array('tj_cash+'=>$order['coin']));		 	
		}
		order_cash_update($id, $order_cash);
		message(1, '处理完成');
	
	}

}
?>